package cn.yc.reader;

import java.io.*;

public class BufferedCopy_2 {
    public static void main(String[] args) {
     /*  拷贝图片 String sourcePicturePath = "C://Users//Yething//Pictures//Screenshots//cyxxtf.jpg";
        String destPicturePath = "E://io//bufferedCopyPicture.jpg";*/

        //拷贝视频
        String sourcePicturePath = "C://Users//Yething//Videos//5月19日//[DLPanda.com][30就拍鸟的ALLEN LAN]7513506201276976419.mp4";
        String destPicturePath = "E://io//bufferedCopyVideo.mp4";
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            // 因为FileInputStream是InputStream的子类
            bis = new BufferedInputStream(new FileInputStream(sourcePicturePath));
            bos = new BufferedOutputStream(new FileOutputStream(destPicturePath));

            // 循环读取文件，并写入到目标位置
            byte[] buf = new byte[1024]; //提升读取效率
            int readLength = 0;
            while ((readLength = bis.read(buf)) != -1) {
                bos.write(buf, 0, readLength);
            }
            System.out.println("拷贝完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭流，关闭外层流，因为会自动关闭底层的字节流

            try {
                if(bis!=null){
                    bis.close();
                }
                if(bos!=null){
                    bos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
